//
//  BTLENotification.swift
//  TimeBoat
//
//  Created by Jun on 2022/9/6.
//  Copyright © 2022 Time Boat. All rights reserved.
//

import Foundation

@objc class BTLENotification: NSObject {
    // MARK: 需要更新蓝牙列表数据
    public static let reloadDeviceListFromCloud = NSNotification.Name.init(rawValue: "reloadDeviceListFromCloud")
    
    // MARK: 广播通知相关定义
    /// 状态更新
    public static let didUpdateStatePeripheral = NSNotification.Name.init(rawValue: "didUpdateState")
    public static let didUpdateStatePeripheralKey = "didUpdateStateKey"
    
    /// 开始搜索设备
    public static let beginSearchPeripheral = NSNotification.Name.init(rawValue: "beginSearchPeripheral")
    
    /// 搜索到设备
    public static let didDiscoverPeripheral = NSNotification.Name.init(rawValue: "didDiscover")
    public static let didDiscoverPeripheralKey = "didDiscoverKey"
    
    /// 停止搜索设备
    public static let stopSearchPeripheral = NSNotification.Name.init(rawValue: "stopSearchPeripheral")
    
    /// 连接成功
    public static let didConnectedPeripheral = NSNotification.Name.init(rawValue: "didConnected")
    public static let didConnectedPeripheralKey = "didConnectedKey"
    
    /// 连接失败
    public static let didConnectFailPeripheral = NSNotification.Name.init(rawValue: "didConnectFail")
    public static let didConnectFailPeripheralKey = "didConnectFailKey"
    
    /// 断开连接
    public static let didDisconnectPeripheral = NSNotification.Name.init(rawValue: "didDisconnect")
    public static let didDisconnectPeripheralKey = "didDisconnectKey"
    
    // MARK: 蓝牙手环数据更新
    public static let didUpdateData = NSNotification.Name.init(rawValue: "didUpdateData")
    public static let didUpdateDataKey = "didUpdateDataKey"
}


extension NotificationCenter {
    /// 重载蓝牙列表数据 - 通知
    func postBTLEReloadDeviceList(object: Any?) {
        post(name: BTLENotification.reloadDeviceListFromCloud, object: object)
    }
    
    /// 重载蓝牙列表数据 - 监听 - 添加
    func addObserverBTLEReloadDeviceList(observer: Any, selector: Selector, object: Any?) {
        addObserver(observer, selector: selector, name: BTLENotification.reloadDeviceListFromCloud, object: object)
    }
    
    /// 重载蓝牙列表数据 - 监听 - 移除
    func removeObserverBTLEReloadDeviceList(observer: Any, object: Any?) {
        removeObserver(observer, name: BTLENotification.reloadDeviceListFromCloud, object: nil)
    }
    
    /// 状态更新 - 通知
    func postBTLEDidUpdateStatePeripheral(object: Any?, info: Any?) {
        var userInfo: [String: Any]? = nil
        if nil != info {
            userInfo = [BTLENotification.didUpdateStatePeripheralKey: info!]
        }
        
        post(name: BTLENotification.didUpdateStatePeripheral, object: object, userInfo: userInfo)
    }
    
    /// 状态更新 - 监听 - 添加
    func addObserverBTLEDidUpdateStatePeripheral(observer: Any, selector: Selector, object: Any?) {
        addObserver(observer, selector: selector, name: BTLENotification.didUpdateStatePeripheral, object: object)
    }
    
    /// 状态更新 - 监听 - 移除
    func removeObserverBTLEDidUpdateStatePeripheral(observer: Any, object: Any?) {
        removeObserver(observer, name: BTLENotification.didUpdateStatePeripheral, object: nil)
    }
    
    /// 开始搜索设备 - 通知
    func postBTLEBeginSearchPeripheral(object: Any?) {
        post(name: BTLENotification.beginSearchPeripheral, object: object, userInfo: nil)
    }
    
    /// 开始搜索设备 - 监听 - 添加
    func addObserverBTLEBeginSearchPeripheral(observer: Any, selector: Selector, object: Any?) {
        addObserver(observer, selector: selector, name: BTLENotification.beginSearchPeripheral, object: object)
    }
    
    /// 开始搜索设备 - 监听 - 移除
    func removeObserverBTLEBeginSearchPeripheral(observer: Any, object: Any?) {
        removeObserver(observer, name: BTLENotification.beginSearchPeripheral, object: object)
    }
    
    /// 搜索到设备 - 通知
    func postBTLEDidDiscoverPeripheral(object: Any?, info: Any?) {
        var userInfo: [String: Any]? = nil
        if nil != info {
            userInfo = [BTLENotification.didDiscoverPeripheralKey: info!]
        }

        post(name: BTLENotification.didDiscoverPeripheral, object: object, userInfo: userInfo)
    }
    
    /// 搜索到设备 - 监听 - 添加
    func addObserverBTLEDidDiscoverPeripheral(observer: Any, selector: Selector, object: Any?) {
        addObserver(observer, selector: selector, name: BTLENotification.didDiscoverPeripheral, object: object)
    }
    
    /// 搜索到设备 - 监听 - 移除
    func removeObserverBTLEDidDiscoverPeripheral(observer: Any, object: Any?) {
        removeObserver(observer, name: BTLENotification.didDiscoverPeripheral, object: object)
    }
    
    /// 停止搜索设备 - 通知
    func postBTLEStopSearchPeripheral(object: Any?) {
        post(name: BTLENotification.stopSearchPeripheral, object: object, userInfo: nil)
    }
    
    /// 停止搜索设备 - 监听 - 添加
    func addObserverBTLEStopSearchPeripheral(observer: Any, selector: Selector, object: Any?) {
        addObserver(observer, selector: selector, name: BTLENotification.stopSearchPeripheral, object: object)
    }
    
    /// 停止搜索设备 - 监听 - 移除
    func removeObserverBTLEStopSearchPeripheral(observer: Any, object: Any?) {
        removeObserver(observer, name: BTLENotification.stopSearchPeripheral, object: object)
    }
    
    /// 连接成功 - 通知
    func postBTLEDidConnectedPeripheral(object: Any?, info: Any?) {
        var userInfo: [String: Any]? = nil
        if nil != info {
            userInfo = [BTLENotification.didConnectedPeripheralKey: info!]
        }

        post(name: BTLENotification.didConnectedPeripheral, object: object, userInfo: userInfo)
    }
    
    /// 连接成功 - 监听 - 添加
    func addObserverBTLEDidConnectedPeripheral(observer: Any, selector: Selector, object: Any?) {
        addObserver(observer, selector: selector, name: BTLENotification.didConnectedPeripheral, object: object)
    }
    
    /// 连接成功 - 监听 - 移除
    func removeObserverBTLEDidConnectedPeripheral(observer: Any, object: Any?) {
        removeObserver(observer, name: BTLENotification.didConnectedPeripheral, object: object)
    }
    
    /// 连接失败 - 通知
    func postBTLEDidConnectFailPeripheral(object: Any?, info: Any?) {
        var userInfo: [String: Any]? = nil
        if nil != info {
            userInfo = [BTLENotification.didConnectFailPeripheralKey: info!]
        }

        post(name: BTLENotification.didConnectFailPeripheral, object: object, userInfo: userInfo)
    }
    
    /// 连接失败 - 监听 - 添加
    func addObserverBTLEDidConnectFailPeripheral(observer: Any, selector: Selector, object: Any?) {
        addObserver(observer, selector: selector, name: BTLENotification.didConnectFailPeripheral, object: object)
    }
    
    /// 连接失败- 监听 - 移除
    func removeObserverBTLEDidConnectFailPeripheral(observer: Any, object: Any?) {
        removeObserver(observer, name: BTLENotification.didConnectFailPeripheral, object: object)
    }
    
    /// 断开连接 - 通知
    func postBTLEDidDisconnectPeripheral(object: Any?, info: Any?) {
        var userInfo: [String: Any]? = nil
        if nil != info {
            userInfo = [BTLENotification.didDisconnectPeripheralKey: info!]
        }

        post(name: BTLENotification.didDisconnectPeripheral, object: object, userInfo: userInfo)
    }
    
    /// 断开连接 - 监听 - 添加
    func addObserverBTLEDidDisconnectPeripheral(observer: Any, selector: Selector, object: Any?) {
        addObserver(observer, selector: selector, name: BTLENotification.didDisconnectPeripheral, object: object)
    }
    
    /// 断开连接- 监听 - 移除
    func removeObserverBTLEDidDisconnectPeripheral(observer: Any, object: Any?) {
        removeObserver(observer, name: BTLENotification.didDisconnectPeripheral, object: object)
    }
    
    /// 数据更新 - 通知
    func postBTLEDidUpdateData(object: Any?, info: Any?) {
        var userInfo: [String: Any]? = nil
        if nil != info {
            userInfo = [BTLENotification.didUpdateDataKey: info!]
        }
        
        post(name: BTLENotification.didUpdateData, object: object, userInfo: userInfo)
    }
    
    /// 数据更新 - 监听 - 添加
    func addObserverBTLEDidUpdateData(observer: Any, selector: Selector, object: Any?) {
        addObserver(observer, selector: selector, name: BTLENotification.didUpdateData, object: object)
    }

    /// 数据更新 - 监听 - 移除
    func removeObserverBTLEDidUpdateData(observer: Any, object: Any?) {
        removeObserver(observer, name: BTLENotification.didUpdateData, object: object)
    }
}

